<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


if(get('get-ajax-content')!=='1'){



if(!isset($USER->JFX_Theme_Default_MenuOpen)) $USER->JFX_Theme_Default_MenuOpen = 0;
$USER->JFX_Theme_Default_MenuOpen = (int)  $USER->JFX_Theme_Default_MenuOpen;


$jsFiles = array(
    'jquery/jquery-1.3.2.min.js',
    'jquery/jquery.cookie.js',
    'jquery/jquery-ui-1.7.2.complete.min.js',
    'jquery/jquery-treeview/jquery.treeview.js',
    'jquery/jquery.form.js',
    'thickbox-compressed.js',
    'json.min.js'//,
//    'codepress/codepress.js'
);

$VIEW->addJsLink($this->jsUrl.'/'.implode(',', $jsFiles));
$VIEW->addJsLink('jquery/jeditable.js');


$code = explode('_', $USER->lang);
$SMARTY->assign('langCode', strtolower($code[1]));
$SMARTY->assign('langTitle', $USER->lang);
$SMARTY->assign('workspaceTitle', $CORE->getContentLang('workspace_title', $USER->workspace));

$cssFiles = array(
    'admin.css',
    'forms.css',
    'admin-menu.css',
    'admin-layout.css',
    'thickbox.css',
    'admin-blocker.css',
    'jquery.treeview.css',
    //'ui-lightness/jquery-ui-1.7.1.custom.css'
    'ui-default-blue/accordion-default-blue.css'
);

$this->addStylesheet(implode(',', $cssFiles));

$VIEW->addEndBody('<script src="'.$CONFIG->baseUrl.'/jfx/codepress/codepress.js" type="text/javascript"></script>');
$VIEW->addEndBody('<script src="'.$CONFIG->jsUrl.'/wz_tooltip/wz_tooltip.js" type="text/javascript"></script>');








$js = <<<JS




function tb_reinit(domChunk) {
	$(domChunk).unbind("click");
	tb_init(domChunk);
}


$(function(){
    // Accordion
    $("#accordion").accordion({ header: "h3", collapsible : 'true', active : {$USER->JFX_Theme_Default_MenuOpen} });

    //$('#jfx-leftcol-menu').animate({left: '-254px'}, 1500);

    //hover states on the static widgets
    $('#dialog_link, ul#icons li').hover(
        function() { $(this).addClass('ui-state-hover'); },
        function() { $(this).removeClass('ui-state-hover'); }
    );

});


function JFX_Theme_Default_MakeAjaxLinks(){
    $('#jfx-workspace-inner a').click(function(){
        if($(this.parentNode).hasClass('redirect')) return true;
        var href = $(this).attr('href');
        if(href.substring(0, 11) === 'javascript:' || href.substring(0, 1) === '#') return;
        JFX_Theme_Default_ShowLoadingAnimation();
        $.getJSON(href, {'get-ajax-content' : '1'}, JFX_Theme_Default_GetAjaxContent);
        return false
    });
};

function JFX_Theme_Default_ShowLoadingAnimation(){
    $('#jfx-workspace-inner').animate({'opacity' : '0'}, 200);

}

function JFX_Theme_Default_HideLoadingAnimation(){
    $('#jfx-workspace-inner').animate({'opacity' : '100'}, 500);
}


function JFX_Theme_Default_GetAjaxContent(json){
    $('#jfx-workspace-inner').html(json._MAIN);
    JFX_Theme_Default_HideLoadingAnimation();
    eval(json._JS);

//    $('head').append(json._HEAD);

    var style = document.createElement('style');
    $(style).html(json._CSS);
    $('#dynamic-style').each(function(){
        this.parentNode.removeChild(this);
    });
    style.id = 'dynamic-style';
    $('head').append(style);
    $('#jfx-workspace-inner form').each(function(){
        var newAction = $(this).attr('action').replace('get-ajax-content=1', '');

        if(newAction.substring(newAction.length-1)==='?'){
            newAction = newAction.substr(0, newAction.length-1);
        }

        if($('input[type=file]', this).get().length > 0){
            // form contains a file field, we cannot ajaxify this one
            this.action = newAction
        }else{
            if(newAction.match('\\\?')){
                newAction += '&get-ajax-content=1';
            }else{
                newAction += '?get-ajax-content=1';
            }
            this.action = newAction;
        }
    });
    $('#jfx-workspace-inner form').submit(function(){
        if($('input[type=file]', this).get().length > 0){
            // contains a file field, submit normally
            return true;
        }
        var ajaxSubmitOptions = {
            'success' : JFX_Theme_Default_AjaxSubmitSuccess,
            'error' : JFX_Theme_Default_AjaxSubmitError,
            'dataType' : 'text',
            'contentType' : 'text/html'
        };

        if($('input[type=file]', this).get().length > 0){
            $(this).ajaxSubmit(ajaxSubmitOptions);
        }else{
            $.post(this.action, $(this).serialize(), JFX_Theme_Default_AjaxSubmitSuccess, 'text');
        }
        return false;
    });

    tb_reinit('a.thickbox, area.thickbox, input.thickbox');

    // make the page tree redirect the whole page instead of load ajax content
    $('#jfx-pages-menu a').each(function(){
        if(!$(this.parentNode).hasClass('redirect')) $(this.parentNode).addClass('redirect');
    });

    $('#TB_window a').each(function(){
        if(!$(this.parentNode).hasClass('redirect')) $(this.parentNode).addClass('redirect');
    });

}


function JFX_Theme_Default_AjaxSubmitSuccess(rsp){
    //alert('success'+rsp);
    try{
        var json = JSON.parse(rsp);
        JFX_Theme_Default_GetAjaxContent(json);
    }catch (e){
        //$.post('{$this->adminUrl}', {'logdata' : rsp});
        //window.location = '{$this->adminUrl}/failed-ajax-submit/';
        alert('caught exception trying to decode response, length: '+rsp.length);
    }
}

function JFX_Theme_Default_AjaxSubmitError(rsp){
    //alert('error'+rsp);
    try{
        var json = eval(rsp);//JSON.parse(rsp);
        JFX_Theme_Default_GetAjaxContent(json);
    }catch (e){
        //$.post('{$CONFIG->adminUrl}', {'logdata' : rsp});
        alert('caught exception trying to decode response, length: '+rsp.length);
    }
}

$(document).ready(function(){

    // save the accordion open status in session via ajax
    $('#jfx-leftcol-menu h3 a').click(function(){
        var id = this.id.replace('jfx-leftcol-menu-', '');
        $.get('{$THEME->adminUrl}/log-menu-open/', {'id' : id});
    });

    // make the page tree redirect the whole page instead of load ajax content
    $('#jfx-pages-menu a').each(function(){
        if(!$(this.parentNode).hasClass('redirect')) $(this.parentNode).addClass('redirect');
    });

    // make each link in the left menu load content via ajax
    $('#jfx-leftcol-menu a').click(function(){
        if($(this.parentNode).hasClass('redirect')) return true;
        var href = $(this).attr('href');
        if(href === 'javascript: void(null)' || href === '#') return;
        JFX_Theme_Default_ShowLoadingAnimation();
        $.getJSON(href, {'get-ajax-content' : '1'}, JFX_Theme_Default_GetAjaxContent);
        return false;
    });

});

var JFXModuleInstallPreMessages = Array();
var JFXModuleUninstallPreMessages = Array();


JS;

$VIEW->addJs($js);

}// end if get('get-ajax-content')==1

$js = <<<JS



$(document).ready(function(){
    setTimeout(CodePress.run, 1100);

    $('#jfx-pages-menu').draggable({handle:'#jfx-pages-menu-drag-handle'});

    $("#jfxcore-draggable-page-tree").treeview({
        persist: "location",
        collapsed: true,
        unique: true
    });

    $("#jfxcore-draggable-page-tree .jfx-delete").click(function(){
        return true;
    });

    $('#jfx-pages').click(function(){
        $('#jfx-pages-menu').fadeIn('slow');
        return false;
    });

    JFX_Theme_Default_MakeAjaxLinks();


    // make the select menus to load content via ajax
    $('#jfx-module-select').each(function(){
        $(this).unbind('change');
        $(this).change(function(e){
            $(this).unbind('change');
            if($('#jfx-lang-select').get().length > 0){
                var langSel = $('#jfx-lang-select').get(0);
                var lang = langSel.options[langSel.selectedIndex].value;
            }else{
                var lang = '';
            }
            var url = '{$CONFIG->adminUrl}/lang/translate/';
            var params = {'lang' : lang, 'module' : this.options[this.selectedIndex].value, 'get-ajax-content' : '1'};
            JFX_Theme_Default_ShowLoadingAnimation();
            $.getJSON(url, params, JFX_Theme_Default_GetAjaxContent);
            return false;
        });
    });

    $('#jfx-lang-select').each(function(){
        $(this).unbind('change');
        $(this).change(function(){
            $(this).unbind('change');
            if($('#jfx-module-select').get().length > 0){
                var moduleSel = $('#jfx-module-select').get(0);
                var module = moduleSel.options[moduleSel.selectedIndex].value;
            }else{
                var module = '';
            }
            var url = '{$CONFIG->adminUrl}/lang/translate/';
            var params = {'module' : module, 'lang' : this.options[this.selectedIndex].value, 'get-ajax-content' : '1'};
            JFX_Theme_Default_ShowLoadingAnimation();
            $.getJSON(url, params, JFX_Theme_Default_GetAjaxContent);
            return false;
        });
    });

    $('#jfx-workspace-select').each(function(){
        $(this).unbind('change');
        $(this).change(function(){
            $(this).unbind('change');
            var url = '{$CONFIG->adminUrl}/workspaces/assign/';
            var params = {'workspace' : this.options[this.selectedIndex].value, 'get-ajax-content' : '1'};
            JFX_Theme_Default_ShowLoadingAnimation();
            $.getJSON(url, params, JFX_Theme_Default_GetAjaxContent);
            return false;
        });
    });

    $('#jfx-workspace-perms-select').each(function(){
        $(this).unbind('change');
        $(this).change(function(){
            $(this).unbind('change');
            var url = '{$CONFIG->adminUrl}/perms/';
            var userSel = $('#jfx-user-perms-select').get(0);
            var userid = userSel.options[userSel.selectedIndex].value;
            var params = {'workspace' : this.options[this.selectedIndex].value, 'user' : userid, 'get-ajax-content' : '1'};
            JFX_Theme_Default_ShowLoadingAnimation();
            $.getJSON(url, params, JFX_Theme_Default_GetAjaxContent);
            return false;
        });
    });


    $('#jfx-user-perms-select').each(function(){
        $(this).unbind('change');
        $(this).change(function(){
            $(this).unbind('change');
            var url = '{$CONFIG->adminUrl}/perms/';
            var workspaceSel = $('#jfx-workspace-perms-select').get(0);
            var workspaceid = workspaceSel.options[workspaceSel.selectedIndex].value;
            var params = {'workspace' : workspaceid, 'user' : this.options[this.selectedIndex].value, 'get-ajax-content' : '1'};
            JFX_Theme_Default_ShowLoadingAnimation();
            $.getJSON(url, params, JFX_Theme_Default_GetAjaxContent);
            return false;
        });
    });





    $('#jfx-workspace-select-to').each(function(){
        $(this).unbind('change');
        $(this).change(function(){
            $(this).unbind('change');
            if($('#jfx-workspace-select-from').get().length > 0){
                var fromSel = $('#jfx-workspace-select-from').get(0);
                var from = fromSel.options[fromSel.selectedIndex].value;
            }else{
                var from = '';
            }
            var url = '{$CONFIG->adminUrl}/workspaces/copy/';
            var params = {'to' : this.options[this.selectedIndex].value, 'from' : from, 'get-ajax-content' : '1'};
            JFX_Theme_Default_ShowLoadingAnimation();
            $.getJSON(url, params, JFX_Theme_Default_GetAjaxContent);
            return false;
        });
    });

    $('#jfx-workspace-select-from').each(function(){
        $(this).unbind('change');
        $(this).change(function(){
            $(this).unbind('change');
            if($('#jfx-workspace-select-to').get().length > 0){
                var toSel = $('#jfx-workspace-select-to').get(0);
                var to = toSel.options[toSel.selectedIndex].value;
            }else{
                var to = '';
            }
            var url = '{$CONFIG->adminUrl}/workspaces/copy/';
            var params = {'from' : this.options[this.selectedIndex].value, 'to' : to, 'get-ajax-content' : '1'};
            JFX_Theme_Default_ShowLoadingAnimation();
            $.getJSON(url, params, JFX_Theme_Default_GetAjaxContent);
            return false;
        });
    });

    $('.jfx-thickbox a').each(function(){
        if(!$(this.parentNode).hasClass('redirect')) $(this.parentNode).addClass('redirect');
    });


});
JS;

$VIEW->addJs($js);
